<template>
	<view v-if="loginStatus">
		<view v-if="isLogin&&requestStatus">
			<view class="personInfo">
				<view class="p-top">
					<view class="p-left">
						<image :src="isRegister?userInfoIntro.imgList[0]:defaultHeader" mode="aspectFill"></image>
					</view>
					<view class="p-right">
						<view class="name" v-if="isRegister">
							{{userInfoIntro.realName}}
						</view>
						<view class="name" v-if="!isRegister">用户{{telephone}}</view>
						<view class="info" v-if="isRegister">
							{{userInfoIntro.address}}-{{userInfoIntro.sex}}-{{userInfoIntro.age}}岁-{{userInfoIntro.department}}-{{userInfoIntro.position}}
						</view>
					</view>
				</view>
				<view class="p-bottom" v-if="isRegister">
					<view class="mylike" @click='gomyLike(myLikeList)'>
						<view class="count">
							{{userInfoIntro.likenum}}
						</view>
						<view class="">
							我喜欢的
						</view>
					</view>
					<view class="shu">
						
					</view>
					<view class="likeme" @click='goLikeme(userInfoIntro.likemeList)'>
						<view class="count">
							{{userInfoIntro.likemeNum}}
						</view>
						<view class="">
							喜欢我的
						</view>
					</view>
				</view>
				<view v-if="!isRegister" class="likeme">
					您还未注册请点击下方~个人资料~进行注册
				</view>
			</view>
			<uni-list>
			    <uni-list-item  title="个人资料" thumb="../../static/资料.png"
			     thumb-size="medium" rightText="修改" showArrow="true" @click="goEditInfo()" link></uni-list-item>
			    <uni-list-item  title="相亲认证" thumb="../../static/资料.png"
			     thumb-size="medium" rightText="1/6" showArrow="true" ></uni-list-item>
			    <uni-list-item  title="兑换码" thumb="../../static/资料.png"
			     thumb-size="medium" rightText="立即兑换" showArrow="true" ></uni-list-item>		    
			</uni-list>
		</view>
		<view v-if="!isLogin">
			您还未登录，将为您跳转登录，请稍后~
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo:{},
				userInfoIntro:{},
				user_id:'',
				telephone:'',
				requestStatus:false,
				isLogin:false,
				isRegister:false,
				defaultHeader:'../../static/logo.jpg',
				loginStatus:false,
				isFirstEnter:true
			}
		},
		onLoad(){
			this.isToken();
		},
		onReady(){
			this.isFirstEnter = false
		},
		onTabItemTap() {
			if (!this.isFirstEnter) {
				this.isToken()
			}
		},
		methods: {
			async isToken(){
				let userInfo = uni.getStorageSync("UserInfo");
				let{token,uid,phone} = userInfo;
				this.user_id = uid
				this.telephone = phone
				var result = await this.$http("/api/checktoken",{},"GET",token).catch(err=>{
					console.log(err);
					return;
				})
				if(result.data.code==403){
					this.loginStatus = true
					uni.showLoading({
						title:'请先登录'
					})
					setTimeout(()=>{
						uni.reLaunch({
							url:'/pages/send/send'
						})
					},1500)
				}
				else{
					this.loginStatus = true
					this.isLogin = true
					this.getUsersInfo()
				}
				
			},
			goEditInfo(){
				uni.navigateTo({
					url:'/pages/editInfo/editInfo'
					})
			},
			getUsersInfo() {
				uni.showLoading({
					title: '正在加载中'
				})
				this.$api.users_allget().then((res) => {
						let userInfoIntroList = res.data.data
						let userInfoIntro = {}
						if(userInfoIntroList){
							for(let i = 0;i < userInfoIntroList.length;i++){
								if(userInfoIntroList[i].user_id == this.user_id){
									userInfoIntro = userInfoIntroList[i]
									this.isRegister = true
								}
							}
							let myLikeNum = 0;
							let likemeNum = 0;
							let myLikeList = [];
							userInfoIntroList.forEach((item)=>{
								if(item.likemeList&&item.likemeList.indexOf(this.user_id) !== -1){
									let index = item.likemeList.indexOf(this.user_id);
									myLikeList.push(item.likemeList[index])
									myLikeNum++
								}
								return myLikeNum
							})
							this.myLikeList = myLikeList;
							// 我喜欢的
							userInfoIntro.likenum = myLikeNum
							userInfoIntro.age = this.jsGetAge(userInfoIntro.birth)
							// 喜欢我的
							if(userInfoIntro.likemeList){
								likemeNum = userInfoIntro.likemeList.length
							}
							userInfoIntro.likemeNum = likemeNum
							this.userInfoIntro = userInfoIntro
							this.requestStatus = true
							uni.hideLoading()
						}
						
					})
					.catch((res) => {
						console.log(res, 'pppppppp')
						this.requestStatus = true
						uni.hideLoading()
					})
			},
			jsGetAge(strBirthday) {
				var returnAge;
				var strBirthdayArr = strBirthday.split("-");
				var birthYear = strBirthdayArr[0];
				var birthMonth = strBirthdayArr[1];
				var birthDay = strBirthdayArr[2];
				let d = new Date();
				var nowYear = d.getFullYear();
				var nowMonth = d.getMonth() + 1;
				var nowDay = d.getDate();
				if (nowYear == birthYear) {
					returnAge = 0; //同年 则为0岁  
				} else {
					var ageDiff = nowYear - birthYear; //年之差  
					if (ageDiff > 0) {
						if (nowMonth == birthMonth) {
							var dayDiff = nowDay - birthDay; //日之差  
							if (dayDiff < 0) {
								returnAge = ageDiff - 1;
							} else {
								returnAge = ageDiff;
							}
						} else {
							var monthDiff = nowMonth - birthMonth; //月之差  
							if (monthDiff < 0) {
								returnAge = ageDiff - 1;
							} else {
								returnAge = ageDiff;
							}
						}
					} else {
						returnAge = -1; //返回-1 表示出生日期输入错误 晚于今天  
					}
				}
				return returnAge; //返回周岁年龄    
			},
			goLikeme(e){
				console.log(e,'eeee')
				let likemeList = e;
				uni.navigateTo({
					url:'/pages/likeme/likeme?list='+likemeList
					})
			},
			gomyLike(e){
				console.log(e,'eeee')
				let myLikeList = e;
				uni.navigateTo({
					url:'/pages/mylike/mylike?list='+myLikeList
					})
			}
			
		}
	}
</script>

<style>
	.personInfo{
		width: 100%;
		height: 270rpx;
		background-color: #f7ce8e;
	}
	.personInfo .p-top{
		overflow: hidden;
	}
	.personInfo .p-left{
		float: left;
		overflow: hidden;
	}
	.personInfo .p-left image{
		width: 116rpx;
		height: 116rpx;
		border-radius: 50%;
		margin: 20rpx 30rpx;
	}
	.personInfo .p-right{
		font-size: 26rpx;
		line-height: 42rpx;
		padding-top: 46rpx;
		color: #fff;
	}
	.personInfo .p-bottom{
		font-size: 24rpx;
		color: #fff;
	}
	.personInfo .p-bottom .shu{
		width: 5rpx;
		height: 36rpx;
		margin-top: 40rpx;
		background-color: #fff;
		float: left;
	}
	.personInfo .p-bottom .likeme,.personInfo .p-bottom .mylike{
		width: 100rpx;
		margin: 20rpx 36rpx;
		float: left;
	}
	.personInfo .p-bottom .count{
		text-align: center;
	}
</style>

